﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    /// <summary>
    /// Jeremy
    /// </summary>
    public partial class MarkForm : Form
    {
        /// <summary>
        /// jhdkfsh
        /// </summary>
        public MarkForm()
        {
            InitializeComponent();
        }

        private void m_Button_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        

        #region Jeremy
        /// <summary>
        /// Hello
        /// </summary>
        /// <param name="sender">Jeremy</param>
        /// <param name="e">Jeremy</param>

        private void m_Button_Load_Click(object sender, EventArgs e)
        {
            DataTable dt = _GetData();

            DataColumn Column = new DataColumn("STT");
            dt.Columns.Add(Column);

            if (dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["STT"] = (i + 1);
                }
            }
            m_DataGridView_MarkView.DataSource = dt;

            double TongDiem = _GetTongDiem(dt);
            int TongChi = _GetTongChi(dt);

            m_TextBox_TinChi.Text = TongChi.ToString();
            m_TextBox_Total.Text = (TongDiem / TongChi).ToString();
        }
        #endregion

        private double _GetTongDiem(DataTable dt)
        {
            double TongDiem = 0;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TongDiem += Convert.ToDouble(dt.Rows[i]["Diem"]) * Convert.ToDouble(dt.Rows[i]["SoChi"]);
            }

            return TongDiem;
        }

        /// <summary>
        /// Lấy tổng chỉ
        /// </summary>
        /// <param name="dt"> Bảng dữ liệu</param>
        /// <returns>Số chỉ</returns>
        private int _GetTongChi(DataTable dt)
        {
            int TongChi = 0;

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                TongChi += Convert.ToInt32(dt.Rows[i]["SoChi"]);
            }

            return TongChi;
        }

        private DataTable _GetData()
        {
            DataTable dt = null;

            string QueryString = _GetQueryString();

            dt = _GetData(QueryString);

            return dt;
        }

        private string _GetQueryString()
        {
            string QueryString = "";

            QueryString = "SELECT * FROM DanhSach";

            return QueryString;
        }

        /// <summary>
        /// Jeremy
        /// </summary>
        /// <param name="QueryString">Jeremy</param>
        /// <returns>Jeremy</returns>
        private DataTable _GetData(string QueryString)
        {
            DataTable dt = new DataTable();

            SqlConnection Connection = new SqlConnection();
            Connection.ConnectionString = _GetConnectionString();
            Connection.Open();

            SqlCommand CMD = new SqlCommand();
            CMD.Connection = Connection;
            CMD.CommandText = QueryString;

            SqlDataAdapter ADP = new SqlDataAdapter(CMD);

            ADP.Fill(dt);

            Connection.Close();
            return dt;
        }


        /// <summary>
        /// addsad
        /// </summary>
        /// <returns>Good</returns>
        public string _GetConnectionString()
        {
            string ConnectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=BangDiem;Integrated Security=True";

            return ConnectionString;


        }

        private void m_DataGridView_MarkView_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            //if (UserAddRow == false)
            //{
            //    return;
            //}

            //DataTable dt = (DataTable)m_DataGridView_MarkView.DataSource;

            //DataGridView q = new DataGridView();

            //if (dt.Rows.Count == 0)
            //{
            //    dt.Rows[0]["STT"] = 1;
            //}
            //else
            //{
            //    dt.Rows[e.RowIndex - 1]["STT"] = dt.Rows.Count;
            //}
            //UserAddRow = false;
        }

        private void m_DataGridView_MarkView_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {

        }

        private void m_DataGridView_MarkView_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            DataTable dt = (DataTable)m_DataGridView_MarkView.DataSource;

            DataGridViewRow Row = m_DataGridView_MarkView.CurrentRow;

            DataRow DataRow = dt.NewRow();

            Row.Cells["m_Column_NO"].Value = e.Row.Index;
        }

        private void m_DataGridView_MarkView_UserDeletedRow(object sender, DataGridViewRowEventArgs e)
        {

        }

        private void m_DataGridView_MarkView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            int Index = e.Row.Index;

            DataTable dt = (DataTable)m_DataGridView_MarkView.DataSource;

            for (int i = Index; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["STT"] = i;
            }

        }

        private void m_Button_Update_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)m_DataGridView_MarkView.DataSource;

            if (dt.Rows.Count == 0)
            {
                return;
            }

            SqlConnection Connection = new SqlConnection();
            Connection.ConnectionString = _GetConnectionString();
            Connection.Open();

            SqlCommand CMD = new SqlCommand();
            CMD.Connection = Connection;

            string TEMP = "";
            try
            {
                TEMP = _GetUpdateString(dt);
            }
            catch (Exception ERROR)
            {
                return;
            }
            CMD.CommandText = "Delete DanhSach";
            CMD.ExecuteNonQuery();

            CMD.CommandText = TEMP;
            CMD.ExecuteNonQuery();

            Connection.Close();

            MessageBox.Show("OKE");

            m_Button_Load_Click(sender, e);
        }

        private string _GetUpdateString(DataTable dt)
        {
            string UpdateString = "";

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UpdateString += _GetUpdateString(dt.Rows[i]) + "\n\r";
            }

            return UpdateString;
        }

        private string _GetUpdateString(DataRow row)
        {
            string UpdateString = "INSERT INTO DanhSach VALUES(" + "N'" + row["TenMonHoc"] + "'," + row["SoChi"] + "," + row["Diem"] + ")";

            return UpdateString;
        }

        private void _Update(string UpdateString)
        {

        }

        /// <summary>
        /// Lấy tổng số chỉ
        /// </summary>
        /// <param name="sender">Người gửi</param>
        /// <param name="e">Sự kiện</param>
        /// <example>Jeremy Luu</example>
        public void MarkForm_Load(object sender, EventArgs e)
        {
            DataTable dt = _GetData();
            DataColumn Column = new DataColumn("STT");
            dt.Columns.Add(Column);

            if (dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["STT"] = (i + 1);
                }
            }
            m_DataGridView_MarkView.DataSource = dt;

            double TongDiem = _GetTongDiem(dt);
            int TongChi = _GetTongChi(dt);

            m_TextBox_TinChi.Text = TongChi.ToString();
            m_TextBox_Total.Text = (TongDiem / TongChi).ToString();

        }

        private void expandableSplitter1_ExpandedChanged(object sender, DevComponents.DotNetBar.ExpandedChangeEventArgs e)
        {

        }

    }
}
